package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.UserMapper;
import com.kgc.entity.User;
import com.kgc.exception.NameAndPasswordException;
import com.kgc.service.UserService;
import com.kgc.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author :qinya
 * @PackageName:com.kgc.service.impl
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @date : 2025/5/15,0:28
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Map<String, Object> userLogin(String loginName, String password) {
        User user = userMapper.selectUserByLoginName(loginName);
        if (user == null || !password.equals(user.getPassword())) {
            throw new NameAndPasswordException();
        }
        String jwt = JwtUtil.createToken(Long.valueOf(user.getId()));
        System.out.println("redis存储的id：" + user.getId().toString());
        redisTemplate.opsForValue().set(user.getId().toString(), jwt, 30, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<>();
        map.put("jwt", jwt);
        map.put("user", user);
        return map;
    }

    @Override
    public PageInfo<User> selectUserByPage(Map<String, Object> params) {
        PageHelper.startPage((int) params.get("pageNum"), (int) params.get("pageSize"), "createDate desc");
        List<User> list = userMapper.selectUserByPage(params);
        return new PageInfo<>(list);
    }

    @Override
    public boolean addUser(User user) {
        return userMapper.insertUser(user) == 1;
    }

    @Override
    public boolean editUserByManager(User user) {
        return userMapper.updateUserByManager(user) == 1;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public User selectUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public boolean checkEmail(String email) {
        return userMapper.checkUserEmail(email) == 1;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public boolean checkLoginName(String loginName) {
        return userMapper.selectUserByLoginName(loginName) == null;
    }

    @Override
    public boolean deleUser(int id) {
        return userMapper.deleteUserByManager(id) == 1;
    }

    @Override
    public int updateUserPassword(int id, String password) {

        return userMapper.updateUserPassword(id, password);
    }


}
